package it.univaq.mwt.xml.epubmanager.presentation;

import it.univaq.mwt.xml.epubmanager.business.impl.packager.EPubPackagerService;
import it.univaq.mwt.xml.epubmanager.business.model.Metadata;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.support.SessionStatus;
import org.springframework.web.context.request.WebRequest;

@Controller
@RequestMapping("/packager")
public class EpubPackagerController {

    private static final String METADATA = "metadata";
    @Autowired
    private EPubPackagerService service;

    @Autowired
    private FormMetadataValidator metadataValidator;

    @InitBinder
    public void initBinder(WebDataBinder binder, WebRequest request) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd/MM/yyyy");
        simpleDateFormat.setLenient(false);
        binder.registerCustomEditor(Date.class, new CustomDateEditor(simpleDateFormat, true));
    }

    @RequestMapping(value = "/create", method = {RequestMethod.GET})
    public String createMetadataStart(Model model) {
        model.addAttribute("metadata", new Metadata());
        return "metadata.createform";
    }

    @RequestMapping(value = "/create", method = {RequestMethod.POST})
    public String createMetadata(@ModelAttribute Metadata metadata, BindingResult bindingResult, HttpSession session) {
        metadataValidator.validate(metadata, bindingResult);
        if (bindingResult.hasErrors()) {
            return "metadata.createform";
        }

        service.createMetadata(metadata);
        session.setAttribute("metadata", metadata);
        return "redirect:/packager/results";
    }

    @RequestMapping(value = "/results", method = {RequestMethod.GET})
    public String result(HttpSession session) {
        Metadata metadata = (Metadata) session.getAttribute("metadata");
        System.out.println(metadata.getIsbn());
        System.out.println(metadata.getTitle());
        System.out.println(metadata.getLanguage());
        return "uploadfiles.createform";
    }

    @RequestMapping("/endsession")
    public String endSessionHandlingMethod(SessionStatus status) {
        status.setComplete();
        return "sessionsattributepage";
    }

    private Metadata getMetadataFromSession(HttpSession session) {
        return (Metadata) session.getAttribute(METADATA);
    }
}
